package com.i_art_engineer.meigong.photo.operate;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;

public class ImageOperateView extends SurfaceView implements SurfaceHolder.Callback,Runnable{

    private List<ImageObject> imgLists = new ArrayList<ImageObject>();

    public List<ImageObject> getTmpImgLists() {
        return tmpImgLists;
    }

    public void setTmpImgLists(List<ImageObject> tmpImgLists) {
        this.tmpImgLists = tmpImgLists;
    }

    private List<ImageObject> tmpImgLists = new ArrayList<ImageObject>();
    private Rect mCanvasLimits;

    public Bitmap getBgBmp() {
        return bgBmp;
    }

    public void setBgBmp(Bitmap bgBmp) {
        this.bgBmp = bgBmp;
//        updateView();
    }

    public Bitmap getRetBmp() {
//        if(retBmp==null)
            saveToBitmap();
        return retBmp;
    }

    public Bitmap retBmp;
    private Bitmap bgBmp;
    private Paint paint = new Paint();
    private Rect real_size = new Rect();
    //	private Context mContext;
    private boolean isMultiAdd = false;// true 代表可以添加多个水印图片（或文字），false 代表只可添加单个水印图片（或文字）
    private float picScale = 0.4f;
    private int index=0;
    public static boolean flag=true;
    public SurfaceHolder surfaceHolder;
    public static Canvas mCanvas;

    public static boolean is_can_edit() {
        return is_can_edit;
    }

    public static void setIs_can_edit(boolean is_can_edit) {
        ImageOperateView.is_can_edit = is_can_edit;
    }

    public static  boolean is_can_edit=false;
    public int getmWidth() {
        return mWidth;
    }

    public void setmWidth(int mWidth) {
        this.mWidth = mWidth;
    }

    public int getmHeight() {
        return mHeight;
    }

    public void setmHeight(int mHeight) {
        this.mHeight = mHeight;
    }

    private int mWidth;
    private int mHeight;

    public int getMvwidth() {
        return mvwidth;
    }

    public void setMvwidth(int mvwidth) {
        this.mvwidth = mvwidth;
    }

    public int getMvheight() {
        return mvheight;
    }

    public void setMvheight(int mvheight) {
        this.mvheight = mvheight;
    }

    private  int mvwidth;
    private int mvheight;
    private Thread mThread;
    /**
     * 设置水印图片初始化大小
     *
     * @param picScale
     */
    public void setPicScale(float picScale) {
        this.picScale = picScale;
    }

    /**
     * 设置是否可以添加多个图片或者文字对象
     *
     * @param isMultiAdd true 代表可以添加多个水印图片（或文字），false 代表只可添加单个水印图片（或文字）
     */
    public void setMultiAdd(boolean isMultiAdd) {
        this.isMultiAdd = isMultiAdd;
    }
    public Context mcontext;
    public ImageOperateView(Context context, Bitmap bmp) {
        super(context);
        bgBmp = bmp;
        mcontext=context;
        surfaceHolder=this.getHolder();
        surfaceHolder.addCallback(this);
        this.setFocusable(true);
    }
    public List<ImageObject> getImgLists() {
        return imgLists;
    }

    public void setImgLists(List<ImageObject> Lists) {
        imgLists.clear();
        if(Lists!=null&&Lists.size()>0)
        {
            for(int i=0;i<Lists.size();i++)
                imgLists.add(Lists.get(i));
        }
//        this.imgLists = imgLists;
    }
    /**
     * 将图片对象添加到View中
     *
     * @param imgObj 图片对象
     */
    public void addItem(ImageObject imgObj) {
        if (imgObj == null) {
            return;
        }
        if (!isMultiAdd && imgLists != null) {
            imgLists.clear();
        }
        imgObj.setSelected(true);
        if (!imgObj.isTextObject) {
            imgObj.setScale(picScale);
        }
        ImageObject tempImgObj = null;
        for (int i = 0; i < imgLists.size(); i++) {
            tempImgObj = imgLists.get(i);
            if (tempImgObj.isSelected()) {
                tempImgObj.setSelected(false);
                if (tempImgObj.isTextObject())
                    onLostEditable((TextObject) tempImgObj);
            }
        }
        imgLists.add(imgObj);
        updateView();
    }
    public void ClearAllItem()
    {
        if(imgLists!=null&&imgLists.size()>0)
        {
            imgLists.clear();

        }
    }
   public void removeAllItem()
   {
       if(imgLists!=null&&imgLists.size()>0)
       {
           for(int i=0;i<imgLists.size();i++)
           {
               ImageObject object=imgLists.get(i);
//               object.FreeAllBitmap();//暂时去掉，否则从其它activity返回的时候会造成空指针情况
           }
       }
       imgLists.clear();
   }
    public void removeItem(ImageObject imgObj) {
        if (imgLists.remove(imgObj)) {
            invalidate();
        }
    }

    protected void onLostEditable(TextObject object) {
        object.setEditable(false);
        if (myListener != null) {
            myListener.onLostEditable(object);
        }
    }
    protected void onLostEditable(ImageObject object) {

        if (myListener != null) {
            myListener.onLostEditable(object);
        }
    }
    public Canvas getmCanvas()
   {
       return mCanvas;
   }
    Bitmap originBitmap=null;
    public Bitmap getCurOriginBitmap()
    {
        return originBitmap;
    }
    /**
     * 画出容器内所有的图像
     */
//    @Override
//    protected void onDraw(Canvas canvas) {
//        Log.e("ImageOperateView","onDraw");
//        super.onDraw(canvas);
//        if(bgBmp==null)
//            return;
//        int sc = canvas.save();
//
//        int dwidth = bgBmp.getWidth();
//        int dheight = bgBmp.getHeight();
//
//         mvwidth = getWidth();
//         mvheight = getHeight();
//
//        mCanvasLimits = new Rect(0, 0, mvwidth, mvheight);
//
//        Matrix matrix = new Matrix();
//
//        float scale;
//        float scalex;
//        float scaley;
//        float dx;
//        float dy;
//
//        if (dwidth <= mvwidth && dheight <= mvheight) {
//            scale = 1.0f;
//            scalex=1.0f;
//            scaley=1.0f;
//        } else {
//            scale = Math.min((float) mvwidth / (float) dwidth, (float) mvheight / (float) dheight);
//
//        }
//
//        dx = Math.round((mvwidth - dwidth * scale) * 0.5f);
//        dy = Math.round((mvheight - dheight * scale) * 0.5f);
//
//        matrix.setScale(scale, scale);
//        matrix.postTranslate(dx, dy);
//        Bitmap tmp=resizeBmp;
//        if(tmp!=null&&!tmp.isRecycled())
//        {
//            tmp.recycle();
//            tmp=null;
//        }
//        if (resizeBmp == null) {
//            resizeBmp = Bitmap.createBitmap(bgBmp, 0, 0, bgBmp.getWidth(), bgBmp.getHeight(), matrix, true);
//        }
//        mWidth=resizeBmp.getWidth();
//        mHeight=resizeBmp.getHeight();
//        real_size.top = 0;
//        real_size.left = 0;
//
//        if (resizeBmp.getWidth() < mvwidth)
//            real_size.left = (mvwidth - resizeBmp.getWidth()) / 2;
//        if (resizeBmp.getHeight() < mvheight)
//            real_size.top = (mvheight - resizeBmp.getHeight()) / 2;
//
//        real_size.right = real_size.left + resizeBmp.getWidth();
//        real_size.bottom = real_size.top + resizeBmp.getHeight();
//
//        canvas.drawBitmap(resizeBmp, real_size.left, real_size.top, paint);
//        drawImages(canvas);
//        canvas.restoreToCount(sc);
//        for (ImageObject ad : imgLists) {
//            if (ad != null && ad.isSelected()) {
//                ad.drawIcon(canvas);
//            }
//        }
//
//    }
    Bitmap resizeBmp;
    public Bitmap getResizeBmp()
    {
        return  resizeBmp;
    }
    public Rect getRealSize() {
        return real_size;
    }

    public void save() {
        ImageObject io = getSelected();
        if (io != null) {
            io.setSelected(false);
            if (io.isTextObject())
                onLostEditable((TextObject) io);
        }
        invalidate();
    }

    /**
     * 根据触控点重绘View
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getPointerCount() == 1) {
            handleSingleTouchManipulateEvent(event);
        } else {
            handleMultiTouchManipulateEvent(event);
        }
//        invalidate();

//        super.onTouchEvent(event);
        return true;
    }

    private boolean mMovedSinceDown = false;
    private boolean mResizeAndRotateSinceDown = false;
    private float mStartDistance = 0.0f;
    private float mStartScale = 0.0f;
    private float mStartRot = 0.0f;
    private float mPrevRot = 0.0f;
    static public final double ROTATION_STEP = 2.0;
    static public final double ZOOM_STEP = 0.01;
    static public final float CANVAS_SCALE_MIN = 0.25f;
    static public final float CANVAS_SCALE_MAX = 3.0f;
    private Point mPreviousPos = new Point(0, 0); // single touch events
    float diff;
    float rot;

    /**
     * 多点触控操作
     *
     * @param event
     */
    private void handleMultiTouchManipulateEvent(MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                float x1 = event.getX(0);
                float x2 = event.getX(1);
                float y1 = event.getY(0);
                float y2 = event.getY(1);
                float delX = (x2 - x1);
                float delY = (y2 - y1);
                diff = (float) Math.sqrt((delX * delX + delY * delY));
                mStartDistance = diff;
                // float q = (delX / delY);
                mPrevRot = (float) Math.toDegrees(Math.atan2(delX, delY));
                for (ImageObject io : imgLists) {
                    if (io.isSelected()) {
                        if (io.isTextObject())
                            onLostEditable((TextObject) io);
                        mStartScale = io.getScale();
                        mStartRot = io.getRotation();
                        break;
                    }
                }
                break;

            case MotionEvent.ACTION_MOVE:
                x1 = event.getX(0);
                x2 = event.getX(1);
                y1 = event.getY(0);
                y2 = event.getY(1);
                delX = (x2 - x1);
                delY = (y2 - y1);
                diff = (float) Math.sqrt((delX * delX + delY * delY));
                float scale = diff / mStartDistance;
                float newscale = mStartScale * scale;
                Log.e("ImageOperateView"," new scale="+newscale);
                rot = (float) Math.toDegrees(Math.atan2(delX, delY));
                float rotdiff = mPrevRot - rot;
                for (ImageObject io : imgLists) {
                    if (io.isSelected() && newscale < 10.0f && newscale > 0.1f) {
                        float newrot = Math.round((mStartRot + rotdiff) / 1.0f);
                        if (Math.abs((newscale - io.getScale()) * ROTATION_STEP) > Math.abs(newrot - io.getRotation())) {
                            io.setScale(newscale);
                        } else {
                            io.setRotation(newrot % 360);
                        }
                        break;
                    }
                }

                break;
        }
    }

    /**
     * 获取选中的对象ImageObject
     *
     * @return
     */
    public ImageObject getSelected() {
        for (ImageObject ibj : imgLists) {
            if (ibj.isSelected()) {
                return ibj;
            }
        }
        return null;
    }

    private long selectTime = 0;
    public ImageObject mImageObject =null;
    /**
     * 单点触控操作
     *
     * @param event
     */
    private void handleSingleTouchManipulateEvent(MotionEvent event) {
        long currentTime = 0;
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:

                mMovedSinceDown = false;
                mResizeAndRotateSinceDown = false;
                int selectedId = -1;
                TextObject clickObj = null;
                if(!is_can_edit())
                    return;
                for (int i = imgLists.size() - 1; i >= 0; --i) {
                    ImageObject io = imgLists.get(i);
                    if (io.contains(event.getX(), event.getY())
                            || io.pointOnCorner(event.getX(), event.getY(), OperateConsts.RIGHTBOTTOM)
                            || io.pointOnCorner(event.getX(), event.getY(), OperateConsts.RIGHTTOP)
                            || io.pointOnCorner(event.getX(), event.getY(), OperateConsts.LEFTTOP)) {
                        if(io.isSelected())
                        {
                            if(io.isTextObject())
                            {
                                ((TextObject)io).setPressCount(((TextObject)io).getPressCount()+1);
                            }
                        }
                        io.setSelected(true);
                        imgLists.remove(i);
                        imgLists.add(io);
                        selectedId = imgLists.size() - 1;
                        selectTime = currentTime;
                        break;
                    }
                }
                if (selectedId < 0) {
                    for (int i = imgLists.size() - 1; i >= 0; --i) {
                        ImageObject io = imgLists.get(i);
                        if (io.contains(event.getX(), event.getY())
                                || io.pointOnCorner(event.getX(), event.getY(), OperateConsts.RIGHTTOP)
                                || io.pointOnCorner(event.getX(), event.getY(), OperateConsts.RIGHTBOTTOM)
                                || io.pointOnCorner(event.getX(), event.getY(), OperateConsts.LEFTTOP)) {
                            io.setSelected(true);
                            imgLists.remove(i);
                            imgLists.add(io);
                            selectedId = imgLists.size() - 1;
                            break;
                        }
                    }
                }
                for (int i = 0; i < imgLists.size(); ++i) {
                    ImageObject io = imgLists.get(i);
                    if (i != selectedId) {
                        if (io.isSelected()) {
                            io.setSelected(false);
                            if (io.isTextObject())
                                onLostEditable((TextObject) io);
                            else
                                onLostEditable(io);
                        }
                    }
                }

                ImageObject io = getSelected();
                if (io != null) {
                    if (io.pointOnCorner(event.getX(), event.getY(), OperateConsts.LEFTTOP)) {
                        boolean isToDel = false;
                        if (myListener != null) {
                            isToDel = myListener.onDelete(getSelected());
                        }
                        if (isToDel) imgLists.remove(io);
                    } else if (io.pointOnCorner(event.getX(), event.getY(), OperateConsts.RIGHTTOP)) {
                        if (myListener != null) {
                            myListener.onRightTopCornerClick(getSelected());
                        }
                    } else if (io.pointOnCorner(event.getX(), event.getY(), OperateConsts.RIGHTBOTTOM)) {
                        if (io.isTextObject()){
                            TextObject tmpText = (TextObject) io;
                            tmpText.setEditable(false);
                            if (myListener != null) {
                                    myListener.onLostEditable(tmpText);
                            }
                        }
                        mResizeAndRotateSinceDown = true;
                        float x = event.getX();
                        float y = event.getY();
                        float delX = x - io.getPoint().x;
                        float delY = y - io.getPoint().y;
                        diff = (float) Math.sqrt((delX * delX + delY * delY));
                        mStartDistance = diff;
                        mPrevRot = (float) Math.toDegrees(Math.atan2(delX, delY));
                        mStartScale = io.getScale();
                        mStartRot = io.getRotation();
                    } else if (io.contains(event.getX(), event.getY())) {
                        mMovedSinceDown = true;
                        mPreviousPos.x = (int) event.getX();
                        mPreviousPos.y = (int) event.getY();
                    }
                }
                updateView();
                break;

            case MotionEvent.ACTION_UP:
                if(!is_can_edit())
                    return;
                Log.e("imageOperateView","up!!!!");
                int upselectedId = -1;
                TextObject upclickObj = null;
                for (int i = imgLists.size() - 1; i >= 0; --i) {
                    ImageObject upio = imgLists.get(i);
                    if (upio.contains(event.getX(), event.getY())) {
//                        if(upio.isTextObject())
//                        {
//                            TextObject to=(TextObject)upio;
////                            to.setPressCount(to.getPressCount()+1);
//                        }
                        if(getSelected()!=null&&!getSelected().isTextObject())
                        {
                            if(myListener!=null)
                            {
                                ImageObject imageObject = (ImageObject)getSelected();
                                myListener.onClick(imageObject);
                            }
                        }
                        if(upio.isTextObject()&&((TextObject)upio).getPressCount()>=1)
                        {
                            if (myListener != null) {
                                if (getSelected()!=null&&getSelected().isTextObject()) {
                                    clickObj = (TextObject) getSelected();
                                    myListener.onClick(clickObj);
                                    clickObj.setPressCount(0);
                                    mMovedSinceDown=false;
                                }
                            }
                        }
                        break;
                    }
                }
                updateView();
                mMovedSinceDown = false;
                mResizeAndRotateSinceDown = false;

                break;

            case MotionEvent.ACTION_MOVE:
                if(!is_can_edit())
                    return;
                Log.e("imageOperateView","move!!!!");
                if (mMovedSinceDown) {
                    int curX = (int) event.getX();
                    int curY = (int) event.getY();
                    int diffX = curX - mPreviousPos.x;
                    int diffY = curY - mPreviousPos.y;
                    mPreviousPos.x = curX;
                    mPreviousPos.y = curY;
                    io = getSelected();
                    if(Math.abs(diffX)>5||Math.abs(diffY)>5) {
                        Log.e("imageOperateView","2move!!!!diffx="+diffX+",diffy="+diffY);
                        if (io.isTextObject())
                            ((TextObject) io).setPressCount(0);
                    }
                    if(Math.abs(diffX)<3&&Math.abs(diffY)<3)
                    {
                        return;
                    }
//                    Log.e("ImageOperatview","index="+index);
                    Point p = io.getPosition();
                    int x = p.x + diffX;
                    int y = p.y + diffY;
                    if (p.x + diffX >= mCanvasLimits.left
                            && p.x + diffX <= mCanvasLimits.right
                            && p.y + diffY >= mCanvasLimits.top
                            && p.y + diffY <= mCanvasLimits.bottom)
                        io.moveBy((int) (diffX), (int) (diffY));
                }
                // 旋转和缩放
                if (mResizeAndRotateSinceDown) {
                    io = getSelected();
                    if(io==null)
                        return;
                    if(io.isTextObject())
                    {
                        ((TextObject)io).setPressCount(0);
                    }
                    float x = event.getX();
                    float y = event.getY();
                    float delX = x - io.getPoint().x;
                    float delY = y - io.getPoint().y;
                    diff = (float) Math.sqrt((delX * delX + delY * delY));
                    float scale = diff / mStartDistance;
                    float newscale = mStartScale * scale;
                    rot = (float) Math.toDegrees(Math.atan2(delX, delY));
                    float rotdiff = mPrevRot - rot;
                    if (newscale < 10.0f && newscale > 0.1f) {
                        float newrot = Math.round((mStartRot + rotdiff) / 1.0f);
                        if (Math.abs((newscale - io.getScale()) * ROTATION_STEP) > Math.abs(newrot - io.getRotation())) {
                            io.setScale(newscale);
                        } else {
                            io.setRotation(newrot % 360);
                        }
                    }
                }
                updateView();
                break;
        }

        cancelLongPress();

    }

    /**
     * 循环画图像
     *
     * @param canvas
     */
    private void drawImages(Canvas canvas) {
        for (ImageObject ad : imgLists) {
            if (ad != null) {
                ad.draw(canvas);
            }
        }
    }

    /**
     * 向外部提供双击监听事件（双击弹出自定义对话框编辑文字）
     */
    OperateListener myListener;

    public void setOnListener(OperateListener myListener) {
        this.myListener = myListener;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
//        ViewGroup.LayoutParams lp = this.getLayoutParams();
//        lp.height = bgBmp.getHeight();
//        lp.width = bgBmp.getWidth();
//        setLayoutParams(lp);
        flag=true;
        Log.e("ImageOperateView","surfaceCreated");
//        mThread=new Thread(this);
//        mThread.start();
//        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.e("ImageOperateView","surfaceChanged");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        flag = false;
        Log.e("ImageOperateView","surfaceDestroy");
//        try {
//            mThread.join();
//            Log.e("ImageOperateView","join");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }

    public void Draw(Canvas mCanvas) {

        if(mCanvas==null)
            return;
         if(bgBmp==null || bgBmp.isRecycled())
             return;
         mCanvas.drawColor(Color.WHITE);
         int dwidth = bgBmp.getWidth();
         int dheight = bgBmp.getHeight();

         mvwidth = getWidth();
         mvheight = getHeight();

         mCanvasLimits = new Rect(0, 0, mvwidth, mvheight);

         Matrix matrix = new Matrix();

         float scale;
         float dx;
         float dy;

         if (dwidth <= mvwidth && dheight <= mvheight) {
             scale = 1.0f;
         } else {
             scale = Math.min((float) mvwidth / (float) dwidth, (float) mvheight / (float) dheight);

         }

         dx = Math.round((mvwidth - dwidth * scale) * 0.5f);
         dy = Math.round((mvheight - dheight * scale) * 0.5f);

         matrix.setScale(scale, scale);
         matrix.postTranslate(dx, dy);
         resizeBmp = Bitmap.createBitmap(bgBmp, 0, 0, bgBmp.getWidth(), bgBmp.getHeight(), matrix, true);
         mWidth=resizeBmp.getWidth();
         mHeight=resizeBmp.getHeight();
         real_size.top = 0;
         real_size.left = 0;

         if (resizeBmp.getWidth() < mvwidth)
             real_size.left = (mvwidth - resizeBmp.getWidth()) / 2;
         if (resizeBmp.getHeight() < mvheight)
             real_size.top = (mvheight - resizeBmp.getHeight()) / 2;

         real_size.right = real_size.left + resizeBmp.getWidth();
         real_size.bottom = real_size.top + resizeBmp.getHeight();

         mCanvas.drawBitmap(resizeBmp, real_size.left, real_size.top, paint);
         drawImages(mCanvas);
//         saveToBitmap();
        for (ImageObject ad : imgLists) {
            if (ad != null && ad.isSelected()) {
                ad.drawIcon(mCanvas);
            }
        }
    }
 public void FreeGarbageBitmap()
 {
     if(bgBmp!=null&&!bgBmp.isRecycled())
     {
         bgBmp.recycle();;
         bgBmp=null;
     }
     if(resizeBmp!=null&&!resizeBmp.isRecycled())
     {
         resizeBmp.recycle();;
         resizeBmp=null;
     }
 }
   public  void FreeBitmap()
   {
       if(bgBmp!=null&&!bgBmp.isRecycled())
       {
           bgBmp.recycle();;
           bgBmp=null;
       }
       if(resizeBmp!=null&&!resizeBmp.isRecycled())
       {
           resizeBmp.recycle();;
           resizeBmp=null;
       }
       if(retBmp!=null&&!retBmp.isRecycled())
       {
           retBmp.recycle();;
           retBmp=null;
       }
       System.gc();
   }
    private void saveToBitmap()
    {
        if(retBmp!=null&&!retBmp.isRecycled())
        {
            retBmp.recycle();
            retBmp=null;
        }
        retBmp=Bitmap.createBitmap(mvwidth,mvheight,resizeBmp.getConfig());
        Canvas canvas=new Canvas();
        canvas.setBitmap(retBmp);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(resizeBmp,real_size.left, real_size.top, paint);
        drawImages(canvas);
    }
   public void updateView() {
       synchronized (surfaceHolder) {
           mCanvas = surfaceHolder.lockCanvas();
           Draw(mCanvas);
           surfaceHolder.unlockCanvasAndPost(mCanvas);
       }
   }
    @Override
    public void run() {
        Log.e("ImageOperateView","run flag="+flag);
        while(flag)
        {
            {
                mCanvas=surfaceHolder.lockCanvas();
                synchronized (surfaceHolder){
                    Draw(mCanvas);
                }
                surfaceHolder.unlockCanvasAndPost(mCanvas);
                try{
                    Thread.sleep(100);
                }catch (InterruptedException e)
                {
                    e.printStackTrace();
                }
            }
        }

    }
    public boolean getFlag()
    {
        return flag;
    }
 public void setFlag(boolean flag)
 {
     this.flag=flag;
//     if(flag)
//     {
//         mThread=new Thread(this);
//         mThread.start();
//     }
//     else {
//         try {
//             mThread.join();
//         }catch (InterruptedException e)
//         {
//             e.printStackTrace();
//         }
//
//     }
     Log.e("imageoperateView", "set flag=" + flag);
 }
    public interface OperateListener {

        public void onClick(TextObject object);
        public void onClick(ImageObject object);
        /**
         * 默认图片对象不显示，但也可以设置为显示，若为文本对象，请自行进行强制类型转换
         *
         * @param object
         */
        public void onRightTopCornerClick(ImageObject object);

        public boolean onDelete(ImageObject object);

        public void onLostEditable(TextObject object);
        public void onLostEditable(ImageObject object);

    }

}